package cn.yh.register.user.api;

import cn.yh.register.common.result.Result;
import cn.yh.register.common.util.AuthContextHolder;
import cn.yh.register.common.util.IpUtil;
import cn.yh.register.model.user.UserInfo;
import cn.yh.register.user.service.UserInfoservice;

import cn.yh.register.vo.user.LoginVo;
import cn.yh.register.vo.user.UserAuthVo;
import com.sun.org.apache.regexp.internal.RE;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(tags = "用户接口")
@RestController
@RequestMapping("/api/user")
public class UserInfoApiController {
    @Autowired
    UserInfoservice userInfoservice;
    @ApiOperation("登录")
    @PostMapping("/login")
    public Result<Map<String, Object>> login(@RequestBody LoginVo loginVo, HttpServletRequest request){
        loginVo.setIp(IpUtil.getIpAddr(request));
        Map<String, Object> map = userInfoservice.login(loginVo);
        return Result.ok(map);

    }


    @ApiOperation("用户认证")
    @PostMapping("/auth/userAuth")
    public Result<?> userAuth(HttpServletRequest request,
                              @RequestBody UserAuthVo userAuthVo){
        Long userId = AuthContextHolder.getUserId(request);
        userInfoservice.userAuth(userId,userAuthVo);
        return Result.ok();
    }
    @ApiOperation("查看用户信息")
    @GetMapping("/auth/getUserInfo")
    public Result<UserInfo> getUserInfo(HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);
        UserInfo userInfo = userInfoservice.getUserInfo(userId);
        return Result.ok(userInfo);
    }
}
